package com.lgCore.server;

import com.lgCore.factory.RpcServiceZkFactory;
import com.lgCore.model.SystemActiveEnum;
import com.lgCore.model.SystemRegionEnum;
import com.lgCore.provide.model.ServiceZkData;
import com.lgCore.server.factory.CommonRpcServiceFactory;
import com.lgCore.util.ClassUtil;
import com.lgCore.util.IpAddr;
import com.lgCore.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class SxRpcTcpServer implements RpcServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(SxRpcTcpServer.class);

    private String zkRegistry;
    private String servicePack;
    private ApplicationContext applicationContext;
    private String systemRegion;
    private String profilesActive;

    public SxRpcTcpServer(String zkRegistry, String servicePack, ApplicationContext applicationContext, String systemRegion, String profilesActive) {
        this.zkRegistry = zkRegistry;
        this.servicePack = servicePack;
        this.applicationContext = applicationContext;
        this.systemRegion = systemRegion;
        this.profilesActive = profilesActive;
    }

    public SxRpcTcpServer(String zkRegistry, String servicePack, ApplicationContext applicationContext, String systemRegion) {
        this.zkRegistry = zkRegistry;
        this.servicePack = servicePack;
        this.applicationContext = applicationContext;
        this.systemRegion = systemRegion;
    }

    @Override
    public void stop() throws Exception {
        //关闭netty
//        CommonRpcServiceFactory.getServerBootStrap().getBoss().shutdownGracefully();
//        CommonRpcServiceFactory.getServerBootStrap().getWorker().shutdownGracefully();
    }

    @Override
    public void start() throws Exception {

//        //初始化service类到zk
//        RpcServiceZkFactory.getCommonServiceServer().connectZookeeper(zkRegistry, sessionTimeOut);
//        //注入service 到zk
//        Set<Class<?>> classSet = ClassUtil.getIntegerClass(servicePack);
//
//
//
//        List<ServiceZkData> serviceZkDataList = new ArrayList<ServiceZkData>();
//        if (classSet != null && classSet.size() > 0) {
//            for (Class<?> t : classSet) {
//                ServiceZkData serviceZkData = new ServiceZkData();
//                serviceZkData.setIp(IpAddr.getLinuxLocalIp());
//                serviceZkData.setNettyPort(CommonRpcServiceFactory.getServerBootStrap().getPort());
//                serviceZkData.setServicePath(t.getName());
//                serviceZkDataList.add(serviceZkData);
//            }
//        }
//        RpcServiceZkFactory.setServiceZkDataList(serviceZkDataList);
//        //service层部署到zookeeper
//        CommonRpcServerService.getInstance().registerService(RpcServiceZkFactory.getServiceZkDataList());
//        //初始化service方法到内存
//        CommonRpcServiceFactory.getCommonRpcRouteService().initServiceMethod(applicationContext, servicePack);
//        //编码解码 kyro方式
//        ProtocolCore.kyroClass();


        //打开zk客户端
        RpcServiceZkFactory.openConnection(zkRegistry, systemRegion, profilesActive);
        //注入service 到zk
        Set<Class<?>> classSet = ClassUtil.getIntegerClass(servicePack);
        List<ServiceZkData> serviceZkDataList = new ArrayList<ServiceZkData>();
        if (classSet != null && classSet.size() > 0) {
            for (Class<?> t : classSet) {
                ServiceZkData serviceZkData = new ServiceZkData();
                serviceZkData.setIp(IpAddr.getLinuxLocalIp());
                serviceZkData.setNettyPort(CommonRpcServiceFactory.getServerBootStrap().getPort());
                serviceZkData.setServicePath(t.getName());
                serviceZkData.setRpcSystemRegion(SystemRegionEnum.getOfType(systemRegion).getType());
                serviceZkData.setProfilesActive(SystemActiveEnum.getOfType(profilesActive).getType());
                serviceZkDataList.add(serviceZkData);
            }
        }
        RpcServiceZkFactory.setServiceZkDataList(serviceZkDataList);
        //初始化配置
        RpcServiceZkFactory.getCommonServiceServer().initConfig(RpcServiceZkFactory.getZkRootPath(), RpcServiceZkFactory.getClient());
        //service层部署到zookeeper
        RpcServiceZkFactory.getCommonServiceServer().registerServer(RpcServiceZkFactory.getServiceZkDataList());
        //初始化service方法到内存
        CommonRpcServiceFactory.getCommonRpcRouteService().initServiceMethod(applicationContext, servicePack);
        //编码解码 kyro方式
//        ProtocolCore.kyroClass();

    }
}
